package com.easyadmin.common.tools;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Paths;
public class CallPythonFromJava {
    public static void main(String[] args) {


        String scriptPath = Paths.get(System.getProperty("user.dir"),
                "src", "main", "resources",
                "python", "test6.py").toString();

        String currentPath = System.getProperty("user.dir");
        try {
            // 使用ProcessBuilder类
            ProcessBuilder processBuilder = new ProcessBuilder("python", currentPath+"\\src\\main\\resources\\python\\stock_store.py");
            //ProcessBuilder processBuilder = new ProcessBuilder("python", scriptPath);
            processBuilder.redirectErrorStream(true); // 将错误输出和标准输出合并
            Process process = processBuilder.start();

            // 读取输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

            // 等待进程结束
            int exitCode = process.waitFor();
            System.out.println("Exited with code : " + exitCode);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}